package com.czy.owner.ui.archive;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.MaintenanceManualAdapter;
import com.czy.owner.api.MaintenanceManualApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.entity.MaintenanceManual;
import com.czy.owner.entity.StrategyListModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.work.WorkShowActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import cn.zhouchaoyuan.excelpanel.ExcelPanel;

import static com.czy.owner.ui.work.WorkShowActivity.TRANSMISSION_WORK_EXTEND_KEY;
import static com.czy.owner.ui.work.WorkShowActivity.TRANSMISSION_WORK_TYPE_KEY;
import static com.czy.owner.ui.work.WorkShowActivity.WorkJumpType.INDEX;
import static com.czy.owner.ui.work.WorkShowActivity.WorkJumpType.MANUAL;

/**
 * Created by King on 2016/7/19 0019.
 * 保养手册
 */
public class MaintenanceManualActivity extends BaseActivity {


    private CarInfo mCarInfo;
    @BindView(R.id.ep_manual)
    ExcelPanel epManual;

    private MaintenanceManualAdapter manualAdapter;

    private List<List<MaintenanceManual.PointBean>> cells;

    private MaintenanceManual maintenanceManual;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_maintenance_manual;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("保养手册");
        getSubTitle().setVisibility(View.GONE);

        if (getIntent().getExtras() == null || (mCarInfo = (CarInfo) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(MaintenanceManualActivity.this, "参数异常!");
            finish();
            return;
        }
        initListener();


    }

    @Override
    protected void updateViews() {
        MaintenanceManualApi maintenanceManualApi = new MaintenanceManualApi(new HttpOnNextListener<MaintenanceManual>() {
            @Override
            public void onNext(MaintenanceManual o) {
                maintenanceManual = o;

                initData();
                showManual();
            }

            @Override
            public void onCacheNext(String string) {
                super.onCacheNext(string);
                BaseResultEntity<MaintenanceManual> cacheData = new Gson().fromJson(string, new TypeToken<BaseResultEntity<MaintenanceManual>>() {
                }.getType());
                maintenanceManual = cacheData.getData();
                initData();
                showManual();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
                Toast.makeText(MaintenanceManualActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, MaintenanceManualActivity.this, UserHelper.getInstance().getUserAccount() + Constants.OWN_CAR_MAINTENANCE_MANUAL + mCarInfo.getCarBrandId());

        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("session", UserHelper.getInstance().getSessionInfoModel(MaintenanceManualActivity.this).getSession());
        requestMap.put("carBrandId", mCarInfo.getCarBrandId());

        maintenanceManualApi.setMap(requestMap);
        HttpManager.getInstance().doHttpDeal(maintenanceManualApi);
    }


    private void initListener() {
    }

    private void initData() {
        cells = new ArrayList<>();


        for (int rowCounter = 0; rowCounter < maintenanceManual.getModels().size(); rowCounter++) {                     //先竖向循环
            List<MaintenanceManual.PointBean> cellList = new ArrayList<>();
            for (int columnCounter = 0; columnCounter < maintenanceManual.getTimeMileage().size(); columnCounter++) {           //后横向循环
                MaintenanceManual.PointBean pointBean = new MaintenanceManual.PointBean();
                pointBean.setModelsId(maintenanceManual.getModels().get(rowCounter).getModelsId());
                pointBean.setTimeMileageId(maintenanceManual.getTimeMileage().get(columnCounter).getTimeMileageId());
                if (maintenanceManual.getPoint().contains(pointBean)) {
                    for (int pointCounter = 0; pointCounter < maintenanceManual.getPoint().size(); pointCounter++) {
                        if (maintenanceManual.getPoint().get(pointCounter).getModelsId() == pointBean.getModelsId()
                                && maintenanceManual.getPoint().get(pointCounter).getTimeMileageId() == pointBean.getTimeMileageId()) {
                            pointBean = maintenanceManual.getPoint().get(pointCounter);
                            pointBean.setRequired(true);
                            break;
                        }
                    }
                }
                cellList.add(pointBean);
            }
            cells.add(cellList);
        }

    }


    private void showManual() {
        manualAdapter = new MaintenanceManualAdapter(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaintenanceManual.PointBean pointBean = (MaintenanceManual.PointBean) v.getTag();
                startActivityForResult(new Intent(MaintenanceManualActivity.this, WorkShowActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, mCarInfo).putExtra(TRANSMISSION_WORK_TYPE_KEY,MANUAL).putExtra(TRANSMISSION_WORK_EXTEND_KEY,"" + pointBean.getManualPointId()), 0);
            }
        });

        manualAdapter.disableHeader();
        manualAdapter.disableFooter();
        epManual.setAdapter(manualAdapter);
        manualAdapter.setAllData(maintenanceManual.getModels(), maintenanceManual.getTimeMileage(), cells);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            setResult(RESULT_OK);
            finish();
        }
    }
}
